/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.bean.transactions;

import com.giant.bean.parametrics.OpCenBean;
import com.giant.entities.InDetOrder;
import com.giant.exceptions.OperacionInvalidaException;
import com.giant.services.IServicioPersistenciaLocal;
import com.giant.services.ServicioPersistencia;
import com.giant.services.transactions.ServiceDetOrderLocal;
import com.giant.services.views.ServiceOrderV;
import com.giant.services.views.ServiceOrderVLocal;
import com.giant.views.InOrderV;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Harold
 */
@ManagedBean
@SessionScoped
public class DetReplicateBean {

    /**
     * Relación con la interfaz que provee los servicios necesarios del vendedor
     */
    @EJB
    private ServiceDetOrderLocal serviceDetOrder;
    @EJB
    private ServiceOrderVLocal serviceOrderV;
    //-----------------------------------------------------------
    // Atributos
    //-----------------------------------------------------------
    /**
     * Representa un nuevo vendedor a ingresar
     */
    private InDetOrder detOrder = new InDetOrder();
    private String param = "";
    private int noOrder;
    private Date dateOrder;
    private double totalOrder = 0;
    private InOrderV selectedOrderV;
    private List<InDetOrder> allDets;
    @EJB
    private IServicioPersistenciaLocal persistence;

    /**
     * Creates a new instance of OpCenBean
     */
    public DetReplicateBean() {
        persistence = new ServicioPersistencia();
    }

    //-----------------------------------------------------------
    // Getters y setters
    //-----------------------------------------------------------
    /**
     * Devuelve el objeto de vendedor actual
     *
     * @return vendedor Vendedor actual
     */
    public InDetOrder getDetOrder() {
        return detOrder;
    }

    /**
     * Modifica al vendedor actual
     *
     * @param vendedor Nuevo vendedor
     */
    public void setDetOrder(InDetOrder det) {
        this.detOrder = det;
    }

    /**
     * Devuelve todos los vendedores del sistema
     *
     * @return vendedores Lista con todos los vendedores del sistema
     */
    public List<InDetOrder> getDetOrders() {
//           if(allDets==null)
//        {
        String query = "SELECT i FROM InDetOrder i WHERE i.idOrder = " + this.noOrder;
        System.out.println(query);
        allDets = persistence.executeObjectSql(query);
//            allDets=serviceDetOrder.getDetOrder();
//        }
        totalDetails();
        return allDets;
    }

    public void criteriaFilter(String sql, String param) {

        Map<String, String> map = new HashMap<String, String>();
        map.put(param, this.param);

        allDets = this.serviceDetOrder.getDetOrderFilter(sql, map);

    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public boolean saveDetOrder() {
        boolean ret = true;
        try {
            serviceDetOrder.agregarDetOrder(detOrder);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Successful", "Successful save"));

        } catch (OperacionInvalidaException ex) {
            Logger.getLogger(OpCenBean.class.getName()).log(Level.SEVERE, null, ex);
            ret = false;
        }

        return ret;
    }

    public int getNoOrder() {
        return noOrder;
    }

    public void setNoOrder(int noOrder) {
        this.noOrder = noOrder;
    }

    public Date getDateOrder() {
        return dateOrder;
    }

    public void setDateOrder(Date dateOrder) {
        this.dateOrder = dateOrder;
    }

    public float totalDetails() {

        float total = 0;

        if (allDets != null) {
            for (int i = 0; i < allDets.size(); i++) {
                total += allDets.get(i).getPriceUnit() * allDets.get(i).getQuantity();
            }
        }
        return total;
    }

    public double getTotalOrder() {
        return totalOrder;
    }

    public void setTotalOrder(double totalOrder) {
        this.totalOrder = totalOrder;
    }

    public String replicateOrder() {
        try {
            serviceOrderV.replicateOrder(selectedOrderV);
        } catch (Exception ex) {
           FacesContext context = FacesContext.getCurrentInstance();
           context.addMessage(null, new FacesMessage("Error", "An exception has been ocurred"));
        }
        
        return "OrReplicate";
    }

    public InOrderV getSelectedOrderV() {
        return selectedOrderV;
    }

    public void setSelectedOrderV(InOrderV selectedOrderV) {
        this.selectedOrderV = selectedOrderV;
    }

      
}
